"""
Using locks, conditions, and semaphores in the with statement
usage:

with some_lock:
    # do something...

is equivalent to:

some_lock.acquire()
try:
    # do something...
finally:
    some_lock.release()
"""
from multiprocessing import Process, Lock


def file_lock(l, i):
    with l:
        with open("test.log", 'a', encoding="utf-8") as f:
            f.write(str(i))


if __name__ == "__main__":
    p_file = []
    l = Lock()
    for i in range(5):
        p = Process(target=file_lock, args=(l, i))
        p.start()
        p_file.append(p)
    for p in p_file:
        p.join()

    with open("test.log", 'r') as f:
        print(f.read())